package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.aspect.annotation.SaveCommonFieldAnno;
import com.sky.aspect.annotation.UpdateCommonFieldAnno;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import org.apache.ibatis.annotations.*;

@Mapper
public interface EmployeeMapper {

    /**
     * 根据用户名查询员工
     * @param username
     * @return
     */
    @Select("select id, name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user from employee " +
            "where username = #{username}")
    Employee getByUsername(String username);

    /**
     * 修改员工信息，通过id修改员工表
     * @param emp
     */
    @UpdateCommonFieldAnno
    void updateById(Employee emp);

    /**
     * 查询员工信息，通过id和旧密码查询员工表
     * @param dto
     * @return
     */
    @Select("select id, name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user from employee " +
            "where id = #{empId} and password = #{oldPassword};")
    Employee getByIdPassword(PasswordEditDTO dto);

    /**
     * 从员工表插入一个员工全部信息
     * @param employee
     */
    @Insert("insert employee(name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user) " +
            "values (#{name}, #{username}, #{password}, #{phone}, #{sex}, #{idNumber}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser});")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void save(Employee employee);

    /**
     * 从员工表查询符合条件的员工信息
     * 如果员工名没有则为查询全部员工信息
     * @param dto
     * @return
     */
    Page<Employee> page(EmployeePageQueryDTO dto);

    /**
     * 根据id从员工表查询一条员工数据
     * @param parseLong
     * @return
     */
    @Select("select id, name, username, password, phone, sex, id_number, status, create_time, update_time, create_user, update_user from employee "
            + "where id = #{parseLong} ")
    Employee getById(long parseLong);
}
